const StoreJS = require('store/dist/store.legacy');
const TokenKey = 'token';
import store from '../store/index';
import router from '@/router';
// 获取TOKEN
export function getToken() {
  return StoreJS.get(TokenKey);
}
// 设置TOKEN
export function setToken(token) {
  return StoreJS.set(TokenKey, token);
}
//  删除TOKEN
export function removeToken() {
  return StoreJS.remove(TokenKey);
}
export function setTabs(data) {
  StoreJS.set('tabsList', data);
}
export function getSiderBar(){
  return StoreJS.get('dx_op_sideBarIndex')
}
export function setSiderBar(data){
  return StoreJS.set('dx_op_sideBarIndex',data)
}
// 找到当前跳转路由的全部参数item
export function findTabsItem(data, val) {
  if (data != null && data.length < 1) return '';
  var tem;
  for (let index = 0; index < data.length; index++) {
    const ele = data[index];
    if (ele.authUrl == val) {
      tem = ele;
      break;
    }
    if (ele.tblMerchantResource && ele.tblMerchantResource.length > 0) {
      for (let j = 0; j < ele.tblMerchantResource.length; j++) {
        const item = ele.tblMerchantResource[j];
        if (item.authUrl == val) {
          tem = item;
          break;
        }
      }
    }
  }
  return tem;
}
// 将tab添加到tabsList中
export function addTabs(data, val) {
  if (data.length < 1) {
    store.commit('SET_TABSLSIT', val);
    return;
  }
  var status = data.findIndex(item => {
    return item.authUrl == val.authUrl;
  });
  if (status == -1) {
    store.commit('SET_TABSLSIT', val);
  }
}
// 移出tab
export function removeTabs(data, val) {
  // 当tab长度为1的时候，拒绝删除
  if (data.length == 1) {
    return '-2';
  }
  var status = data.findIndex(item => {
    return item.authUrl == val;
  });
  // 判断tab中是否存在当前删除的数据，如果进行删除
  if (status != -1) {
    store.commit('REMOVE_TABSLSIT', status);
    if (val == StoreJS.get('dx_op_sideBarIndex')) {
      var index = status > data.length - 1 ? data.length - 1 : status;
      router.push(`${data[index].authUrl}`);
      store.commit('SET_BAR', data[index].authUrl);
      // StoreJS.set('dx_op_sideBarIndex',data[index].authUrl)
    }
  }
  // return status;
}
